<?php
namespace Home\Controller;
use Think\Controller;

/**
 *会员升级支付
 */
class PayController extends InitController {
	
	//微信支付显示内容
	public function wx_content(){
		$this->display('wx_content');
	}
	
	//微信支付(升级会员)
	public function wx_pay(){
		
		$this->display('wx_pay');
	}
	
	//微信会员权益
	public function member_privilege(){
		$this->display('member_privilege');
	}
	
	//请求支付
    public function Index(){
        $level = I('level');
		$pay_type = I('pay_type');		//目前只有微信支付

		if(!empty($level)){
			$price = M('Level')->where(array('level_id'=>$level))->field('price')->getField('price');
		
			if(!empty($price)){
				$data = array(
					'uid'=>$this->uid,
					'price'=>$price,
					'status'=>0,
					'payment'=>$pay_type,
					'createtime'=>NOW_TIME,
				);
			
				$pay_order_id = M('UserOrder')->add($data);
			}
		}else{
			$this->error("服务器肯定是在开小差了～",U('Index/index'));
		}

		//查询当前会员等级
		$user_info = M('User')->where(array('id'=>$this->uid))->field("level,valid_time")->find();

   		if($user_info['level'] > 0 and $user_info['valid_time'] === '0'){
   			$this->error("你已经是永久会员了，不需要购买",U('Index/index'));
   		}

        // 记录本次支付编号
        $pay_id = insertPaysn($this->uid);
        $title = '会员升级付费';
        $payment_type = C('payment_type');
        if($payment_type[$pay_type]){
			//$level   升级id
            $url =  sendpay_url($this->uid,$pay_order_id, $data['price'], $title, $pay_type, 'home', $pay_id,$level);
			if(IS_AJAX){
				$this->ajaxReturn(array('status' => '1', 'msg' => '请求成功','url'=>$url));
			}
        }else{
            $this->error("服务器肯定是在开小差了～",U('Index/index'));
        }
    }

}